package com.yunxi.service.dubboService.impl.otd;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.otd.OtdWeekCalendar;
import com.yunxi.otd.pojo.OtdWeekCalendarImportVO;
import com.yunxi.otd.pojo.OtdWeekCalendarVO;
import com.yunxi.otd.service.OtdWeekCalendarDubboService;
import com.yunxi.service.localService.otd.OtdWeekCalendarService;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

//@Service(version = "${demo.service.version}",
//    application = "${dubbo.application.id}",
//    protocol = "${dubbo.protocol.id}",
//    registry = "${dubbo.registry.id}"
//)
public class OtdWeekCalendarDubboServiceImpl implements OtdWeekCalendarDubboService {

    @Autowired
    @Qualifier(value = "otdWeekCalendarServiceImpl")
    OtdWeekCalendarService otdWeekCalendarService;

    @Override
    public PageInfo pageByVO(OtdWeekCalendarVO otdWeekCalendarVO, Integer pageNum, Integer pageSize) {
        return otdWeekCalendarService.pageByVo(otdWeekCalendarVO, pageNum, pageSize);
    }

    /**
     * 新增
     *
     * @param weekCalendarVO 周生产日历VO
     */
    @Override
    public ApiResult addWeekCalendar(OtdWeekCalendarVO weekCalendarVO) {
        // 周数补充0
        if (weekCalendarVO.getWeek().length() == 1) {
            weekCalendarVO.setWeek("0" + weekCalendarVO.getWeek());
        }

        // 校验周是否已存在
        OtdWeekCalendar calendar = otdWeekCalendarService
            .findCalendarByWeek(weekCalendarVO.getYear(), weekCalendarVO.getWeek());
        if (calendar != null) {
            return ResponseUtil.fail(-1,"该周日历已存在！");
        }

        // 校验开始时间小于结束时间
        if(weekCalendarVO.getStartDate().after(weekCalendarVO.getEndDate())){
            return ResponseUtil.fail(-1,"开始时间不能大于结束时间！");
        }

        //VO转Bean
        calendar = new OtdWeekCalendar();
        BeanUtils.copyProperties(weekCalendarVO, calendar);
        //校验开始结束时间是否冲突
        Integer isConflict = otdWeekCalendarService
            .checkConflict(weekCalendarVO.getStartDate(), weekCalendarVO.getEndDate(), null);
        if (isConflict > 0) {
            return ResponseUtil.fail(-1,"与其他周开始、结束时间发生冲突！");
        }
        //保存
        otdWeekCalendarService.save(calendar);
        return ResponseUtil.ok();
    }

    /**
     * 修改
     *
     * @param weekCalendarVO 周生产日历VO
     */
    @Override
    public ApiResult modifyWeekCalendar(OtdWeekCalendarVO weekCalendarVO) {
        //校验开始结束时间是否冲突
        Integer isConflict = otdWeekCalendarService
            .checkConflict(weekCalendarVO.getStartDate(), weekCalendarVO.getEndDate(), weekCalendarVO.getCalendarId());
        if (isConflict > 0) {
            return ResponseUtil.fail(-1,"与其他周开始、结束时间发生冲突！");
        }

        OtdWeekCalendar weekCalendar = new OtdWeekCalendar();
        BeanUtils.copyProperties(weekCalendarVO, weekCalendar);
        // 修改
        boolean result = otdWeekCalendarService.updateById(weekCalendar);
        if (!result) {
            return ResponseUtil.fail(-1,"请查询后重试！");
        }
        return ResponseUtil.ok();
    }

    /**
     * 根据ID查询周日历VO
     *
     * @param calendarId ID
     * @return 周日历VO
     */
    @Override
    public OtdWeekCalendarVO getById(String calendarId) {
        OtdWeekCalendarVO calendarVO = new OtdWeekCalendarVO();
        OtdWeekCalendar calendar = otdWeekCalendarService.getById(calendarId);
        BeanUtils.copyProperties(calendar, calendarVO);
        return calendarVO;
    }

    /**
     * 根据ID删除日历
     *
     * @param calendarIdList 日历ID集合
     */
    @Override
    public ApiResult removeCalendarByIds(List<String> calendarIdList) {
        otdWeekCalendarService.removeByIds(calendarIdList);
        return ResponseUtil.ok();
    }

    /**
     * 导入周生产工作日历
     *
     * @param importVOList 导入数据
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult importData(List<OtdWeekCalendarImportVO> importVOList) {
        for (OtdWeekCalendarImportVO importVO : importVOList) {
            if (importVO.getMonth().length() == 1) {
                importVO.setMonth("0" + importVO.getMonth());
            }
            if (importVO.getWeek().length() == 1) {
                importVO.setWeek("0" + importVO.getWeek());
            }

            // 根据年月周查询日历对象
            OtdWeekCalendar calendar = otdWeekCalendarService
                .findCalendarByWeek(importVO.getYear(), importVO.getWeek());
            //校验开始结束时间是否冲突
            Integer isConflict = otdWeekCalendarService
                .checkConflict(importVO.getStartDate(), importVO.getEndDate(),
                    calendar == null ? null : calendar.getCalendarId());
            if (isConflict > 0) {
                return ResponseUtil.fail(-1,"与其它周开始、结束时间发生冲突！");
            }
            // 判断新增还是修改
            if (calendar == null) {
                // 新增
                //VO转Bean
                calendar = new OtdWeekCalendar();
                BeanUtils.copyProperties(importVO, calendar);
                otdWeekCalendarService.save(calendar);
            } else {
                // 修改
                calendar.setStartDate(importVO.getStartDate());
                calendar.setEndDate(importVO.getEndDate());
                boolean result = otdWeekCalendarService.updateById(calendar);
                if (!result) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return ResponseUtil.fail(-1,"更新失败，请查询后重试！");
                }
            }
        }
        return ResponseUtil.ok();
    }


    /**
     * 年月周获取日历
     *
     * @param year 年
     * @param week 周
     * @return 周日历VO
     */
    @Override
    public OtdWeekCalendarVO getByWeek(String year, String week) {
        OtdWeekCalendarVO vo = new OtdWeekCalendarVO();
        OtdWeekCalendar calendar = otdWeekCalendarService.findCalendarByWeek(year, week);
        if (calendar == null) {
            return null;
        }
        BeanUtils.copyProperties(calendar, vo);
        return vo;
    }
}
